package com.youko.backstage.service.commodity;

import com.youko.backstage.bean.commodity.CommoditySku;
import com.youko.backstage.mapper.commodity.CommoditySkuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: youko
 * @classname: CommoditySkuService
 * @Description: 商品Sku service层
 * @date: 2022/7/5 11:18
 */
@Service
public class CommoditySkuService {
    @Autowired
    CommoditySkuMapper commoditySkuMapper;

    public int insertSku(CommoditySku commoditySku){
        commoditySkuMapper.insertSku(commoditySku);
        int skuId = commoditySku.getId();
        return skuId;
    }

    public int[] selectSpuByShop(int shopId){
        List<CommoditySku> skus = commoditySkuMapper.selectAllSkuByMerchantId(shopId);
        int[] spuIdss=new int[skus.size()];
        for (int i=0;i<skus.size();i++){
            spuIdss[i]= skus.get(i).getSpuId();
        }
        int index = 0 ;
        spuIdss[index] = spuIdss[0];
        for(int i = 1 ; i < spuIdss.length ; i++){
            if( spuIdss[index] != spuIdss[i]){
                spuIdss[++index] = spuIdss[i];
            }
        }
        index++ ;
        int[] spuIds=new int[index];
        for(int i = 0 ; i<index ; i++ ) {
            spuIds[i]=spuIdss[i];
        }

        return spuIds;
    }


}
